/**
 * 
 */
package com.yunyike.controller.order;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yunyike.constants.CommonConstant;
import com.yunyike.constants.ExcelConstants;
import com.yunyike.constants.RequestConstant;
import com.yunyike.constants.ResponseConstant;
import com.yunyike.controller.BaseController;
import com.yunyike.model.Quotation;
import com.yunyike.service.QuotationService;
import com.yunyike.utils.QuotationMerchandiseExcelUtil;

@Controller
@RequestMapping(value=RequestConstant.QUOTATIONORDER)
public class OrderController extends BaseController{

	private Logger log = LoggerFactory.getLogger(OrderController.class);
	
	@Autowired
	private QuotationService quotationService;
	
	
	@RequestMapping(value=RequestConstant.ORDERLIST+"/{quotationId}")
	public String toOrderList(HttpServletRequest request, HttpServletResponse response,
			@PathVariable(value="quotationId") String quotationId) {
		log.info("QUOTATIONORDER toOrderList --进入excel下单列表页--quotationId="+quotationId);
		if(StringUtils.isBlank(quotationId)){
			request.setAttribute("message", "请求参数有误，稍后重试");
			log.info("参数有误quotationId为空");
			return ResponseConstant.ORDERLIST;
		}
		request.setAttribute("quotationId", quotationId);
		try {
			Quotation quotation = quotationService.selectByPrimaryKey(quotationId);
			if (quotation != null) {
				String excelName = quotation.getQuotationLink().replace(ExcelConstants.EXCEL_SAVEPATH, "");
				Map result = QuotationMerchandiseExcelUtil.readExcel(ExcelConstants.EXCEL_SAVEPATH, excelName);
				String message = result.get("message") + "";
				if ("".equals(message) || "null".equals(message)) {
					log.info("读取原excel表 pic_col="+result.get("pic_col")+",qty_col="+ result.get("qty_col"));
					request.setAttribute("titles", (String[]) result.get("titles"));
					request.setAttribute("rows", (List<String[]>) result.get("rows"));
					request.setAttribute("pic_col", (String) result.get("pic_col"));
					request.setAttribute("qty_col", (String) result.get("qty_col"));
					request.setAttribute("retail_col", (String) result.get("retail_col"));
					return ResponseConstant.ORDERLIST;
				}
				request.setAttribute("message", message);
			} else {
				request.setAttribute("message", "请求参数有误，稍后重试");
				log.info("参数有误quotationId去数据库找不到数据");
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("message", "发生异常");
		}
		return ResponseConstant.ORDERLIST;
	}
	
	@RequestMapping(value = RequestConstant.PLACEONORDER + "/{quotationId}")
	public String placeOnOrder(String orders, HttpServletRequest request, HttpServletResponse response,
			@PathVariable(value = "quotationId") String quotationId) {
		log.info("QUOTATIONORDER placeOnOrder 下单...");
		if(StringUtils.isBlank(orders)||StringUtils.isBlank(quotationId)){
			log.info("参数有误quotationId为空:quotationId:"+quotationId+",orders:"+orders);
			return "请求参数有误，稍后重试";
		}
		int qty_col = Integer.parseInt(request.getParameter("qty_col"));
		int pic_col = Integer.parseInt(request.getParameter("pic_col"));
		int retail_col = Integer.parseInt(request.getParameter("retail_col"));
		
		try {
			Quotation quotation = quotationService.selectByPrimaryKey(quotationId);
			String excelName = quotation.getQuotationLink().replace(ExcelConstants.EXCEL_SAVEPATH, "");
			String[] data = orders.split("_");
			String message = QuotationMerchandiseExcelUtil
					.importOrder(response,ExcelConstants.EXCEL_SAVEPATH, 
							excelName, data,qty_col,pic_col,retail_col);
			if(!"".equals(message)){
				return message;
			}
			if(CommonConstant.NUMBER_5>quotation.getQuotationStatus()){
				quotation.setQuotationStatus(CommonConstant.NUMBER_5);
				quotationService.updateByPrimaryKeySelective(quotation);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "发生异常";
		}
		
		log.info("QUOTATIONORDER placeOnOrder 下单.END..");
		return ResponseConstant.ORDERLIST;
	}
	
}
